﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace SaveObjectToDatabase
{
    [Serializable]
    public class ObjectToSave
    {
        private int cardNo;
        private string name;

        public ObjectToSave()
        {
            //do nothing here
        }

        [DefaultValue(0)]
        public int CardNo
        {
            get { return cardNo; }
            set { cardNo = value; }
        }

        [TypeConverter(typeof(Int32Converter)),
        DefaultValue("")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public void SaveProperties(DataTable propertiesToFill, bool isTrue)
        {
            //  Only add the class property if we have too.  addClassProperty will be false if we are being called from
            //  a derived class, in which case we don't want to add a __ClassName property for this class (the parent).
            if (isTrue)
            {
                AddPropertyValue(propertiesToFill, string.Empty /* PropertyPath */ , "__ClassName", this.GetType().FullName);
            }

            AddPropertyValue(propertiesToFill, string.Empty /* PropertyPath */ , "CardNo", CardNo);
            AddPropertyValue(propertiesToFill, string.Empty /* PropertyPath */ , "Name", Name);
        }

        public static void AddPropertyValue(DataTable table, string propertyPath, string propertyName, object propertyValue, string propertyType, string category)
        {
            if (table == null)
            {
                Debug.Fail("Invalid table passed to addProperty().");
            }
            else
            {
                int order = 1;
                if (table.Rows.Count > 0)
                {
                    order = (int)table.Select("PropertyOrder = Max(PropertyOrder)")[0]["PropertyOrder"] + 1;
                }

                DataRow dr = table.NewRow();
                dr["PropertyPath"] = propertyPath;
                dr["PropertyName"] = propertyName;
                dr["PropertyValue"] = propertyValue;
                dr["PropertyType"] = propertyType;
                dr["PropertyOrder"] = order;
                dr["Category"] = category;
                table.Rows.Add(dr);
                table.AcceptChanges();
            }
        }

        public static void AddPropertyValue(DataTable table, string propertyPath, string propertyName, object propertyValue)
        {
            AddPropertyValue(table, propertyPath, propertyName, propertyValue, string.Empty /* no category */ );
        }

        public static void AddPropertyValue(DataTable table, string propertyPath, string propertyName, object propertyValue, string category)
        {
            AddPropertyValue(table, propertyPath, propertyName, propertyValue, string.Empty /* type */ , category);
        }
    }
}
